//
//  HttpDatas.m
//  shuai
//
//  Created by xingshuai on 2019/7/13.
//  Copyright © 2019 xingshuai. All rights reserved.
//

#import "HttpDatas.h"

#import "RequestDatas.h"

@implementation HttpDatas

/** 文件上传，获取转换后的路径url（图片） */
+(void)requestImgUploadFile:(UIImage *)file success:(void(^)(id json))success failure:(void(^)(NSError *err))failure
{
    NSData *imageData;
    NSString *imageFormat;
    if (UIImagePNGRepresentation(file) != nil) {
        imageFormat = @"Content-Type: image/png \r\n";
        imageData = UIImagePNGRepresentation(file);
        
    }else{
        imageFormat = @"Content-Type: image/jpeg \r\n";
        imageData = UIImageJPEGRepresentation(file, 0.7);
    }
    NSURL *url = [NSURL URLWithString:@"http://101.132.114.122:8099/upload"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"POST";
    //设置请求实体
    NSMutableData *body = [NSMutableData data];
    
    //文件参数
    [body appendData:[self getDataWithString:@"--BOUNDARY\r\n" ]];
    NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"file.jpg\"\r\n"];
    [body appendData:[self getDataWithString:disposition ]];
    [body appendData:[self getDataWithString:imageFormat]];
    [body appendData:[self getDataWithString:@"\r\n"]];
    [body appendData:imageData];
    [body appendData:[self getDataWithString:@"\r\n"]];
    //普通参数
    [body appendData:[self getDataWithString:@"--BOUNDARY\r\n" ]];
    //上传参数需要key： （相应参数，在这里是file）
    NSString *dispositions = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n",@"key"];
    [body appendData:[self getDataWithString:dispositions ]];
    [body appendData:[self getDataWithString:@"\r\n"]];
    [body appendData:[self getDataWithString:@"file"]];
    [body appendData:[self getDataWithString:@"\r\n"]];
    
    //参数结束
    [body appendData:[self getDataWithString:@"--BOUNDARY--\r\n"]];
    request.HTTPBody = body;
    //设置请求体长度
    NSInteger length = [body length];
    [request setValue:[NSString stringWithFormat:@"%ld",length] forHTTPHeaderField:@"Content-Length"];
    //设置 POST请求文件上传
    [request setValue:@"multipart/form-data; boundary=BOUNDARY" forHTTPHeaderField:@"Content-Type"];
    
    //运用AFN实现照片上传
    AFHTTPSessionManager *manager = [self manager];
    
    NSDictionary *dict = @{@"file":file};
    
    [manager POST:@"http://101.132.114.122:8099/upload" parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        [formData appendPartWithFileData:imageData name:@"file" fileName:[NSString stringWithFormat:@"file.jpg"] mimeType:@"image/jpeg"];
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if(success)
        {
            success(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if(failure)
        {
            failure(error);
        }
        
    }];
}


+(NSData *)getDataWithString:(NSString *)string{
    
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    
    return data;
    
}


+(AFHTTPSessionManager*) manager
{
    NSURL *mybaseURL = [NSURL URLWithString:shuai_baseUATURL];
    // 1.创建请求管理对象
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:mybaseURL];
    // 2.过滤NSNull参数
    ((AFJSONResponseSerializer *)manager.responseSerializer).removesKeysWithNullValues = YES;
    
    // 3.请求的数据和接收的数据格式
    manager.responseSerializer =[AFJSONResponseSerializer serializer];
    [manager.requestSerializer setValue:@"multipart/form-data" forHTTPHeaderField:@"Content－Type"];
//    manager.requestSerializer  = [AFHTTPRequestSerializer serializer];
    // 4.设置请求成功后的接受内容的类型
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", @"text/plain",@"application/json", @"image/jpeg",@"image/png",nil];
    manager.requestSerializer.timeoutInterval = 10;
    
    return manager;
}


/** 注册 */
+(void)requestRegistUser_type:(NSString *)user_type name:(NSString *)name email:(NSString *)email password:(NSString *)password photo:(NSString *)photo success:(void(^)(id json))success failure:(void(^)(NSError *err))failure
{
    NSDictionary* parmas = @{
                             @"user_type":user_type,
                             @"name":name,
                             @"email":email,
                             @"password":password,
                             @"photo":photo
                             };
    
    [RequestDatas requestWithMethod:RequestMethodTypePost withUrl:@"/user/register" withIsPullDown:YES withParameter:parmas success:success failure:failure];
}

/** 登陆 */
+(void)requestLoginUser_type:(NSString *)user_type email:(NSString *)email password:(NSString *)password success:(void(^)(id json))success failure:(void(^)(NSError *err))failure
{
    NSDictionary* parmas = @{
                             @"user_type":user_type,
                             @"email":email,
                             @"password":password,
                             };
    
    [RequestDatas requestWithMethod:RequestMethodTypePost withUrl:@"/user/login" withIsPullDown:YES withParameter:parmas success:success failure:failure];
}

/** 首页公开列表 */
+(void)requestHomeTOken:(NSString *)token page:(NSInteger)page pageSize:(NSInteger)pageSize success:(void(^)(id json))success failure:(void(^)(NSError *err))failure
{
    NSDictionary* parmas = @{
                             @"token":token,
                             @"page":@(page),
                             @"pageSize":@(pageSize),
                             };
    
    [RequestDatas requestWithMethod:RequestMethodTypeGet withUrl:@"/user/public_user_list" withIsPullDown:YES withParameter:parmas success:success failure:failure];
}

/** 当前页卡详细信息 */
+(void)requestInforHomeToken:(NSString *)token user_id:(NSString *)user_id success:(void(^)(id json))success failure:(void(^)(NSError *err))failure
{
    NSDictionary* parmas = @{
                             @"token":token,
                             @"user_id":user_id,
                             };
    
    [RequestDatas requestWithMethod:RequestMethodTypePost withUrl:@"/home/user_info" withIsPullDown:YES withParameter:parmas success:success failure:failure];
}

/** 首页用户操作 */
+(void)requestHomeToken:(NSString *)token user_id:(NSString *)user_id type:(NSString *)type success:(void(^)(id json))success failure:(void(^)(NSError *err))failure
{
    NSDictionary* parmas = @{
                             @"token":token,
                             @"user_id":user_id,
                             @"type":type
                             };
    
    [RequestDatas requestWithMethod:RequestMethodTypePost withUrl:@"/user/operation" withIsPullDown:YES withParameter:parmas success:success failure:failure];
}

/** 个人中心（获取） */
+(void)requestOneSelfDataHomeToken:(NSString *)token success:(void(^)(id json))success failure:(void(^)(NSError *err))failure
{
    NSDictionary* parmas = @{
                             @"token":token
                             };
    
    [RequestDatas requestWithMethod:RequestMethodTypeGet withUrl:@"/user/personal" withIsPullDown:YES withParameter:parmas success:success failure:failure];
}

/** 个人中心（保存） */
+(void)requestSaveOneSelfDataToken:(NSString *)token photo:(NSString *)photo photo1:(NSString *)photo1 photo2:(NSString *)photo2 photo3:(NSString *)photo3 user_name:(NSString *)user_name age:(NSString *)age work:(NSString *)work bio:(NSString *)bio age_min:(NSString *)age_min age_max:(NSString *)age_max success:(void(^)(id json))success failure:(void(^)(NSError *err))failure
{
    NSDictionary* parmas = @{
                             @"token":token,
                             @"photo":photo,
                             @"photo1":photo1,
                             @"photo2":photo2,
                             @"photo3":photo3,
                             @"user_name":user_name,
                             @"age":age,
                             @"work":work,
                             @"bio":bio,
                             @"age_min":age_min,
                             @"age_max":age_max
                             };
    
    [RequestDatas requestWithMethod:RequestMethodTypePost withUrl:@"/user/user_save" withIsPullDown:YES withParameter:parmas success:success failure:failure];
}


/** 好友列表 */
+(void)requestFriendDataToken:(NSString *)token success:(void(^)(id json))success failure:(void(^)(NSError *err))failure
{
    NSDictionary* parmas = @{
                             @"token":token
                             };
    
    [RequestDatas requestWithMethod:RequestMethodTypeGet withUrl:@"/message/message_list" withIsPullDown:YES withParameter:parmas success:success failure:failure];
}


/** 收藏 喜欢列表 */
+(void)requestCollectFriendDataToken:(NSString *)token success:(void(^)(id json))success failure:(void(^)(NSError *err))failure
{
    NSDictionary* parmas = @{
                             @"token":token
                             };
    
    [RequestDatas requestWithMethod:RequestMethodTypeGet withUrl:@"/message/collect_list" withIsPullDown:YES withParameter:parmas success:success failure:failure];
}

/** 聊天信息 */
+(void)requestImDataDataToken:(NSString *)token user_id:(NSString *)user_id success:(void(^)(id json))success failure:(void(^)(NSError *err))failure
{
    NSDictionary* parmas = @{
                             @"token":token,
                             @"user_id":user_id
                             };
    
    [RequestDatas requestWithMethod:RequestMethodTypeGet withUrl:@"/message/message" withIsPullDown:YES withParameter:parmas success:success failure:failure];
}




@end
